﻿Imports System.Data
Imports ADOEntityKYC

Partial Class KYC_NonIndividualReasonPopup
    Inherits System.Web.UI.Page
    Dim clsValid As clsValidateData
    Dim clsSessionMenuRight As clsSessionMenuRight = New clsSessionMenuRight
    Dim dtReason As New DataTable
    Dim KYCEntity As KYCEntities = New KYCEntities
    Dim CustSeq As Integer = 0

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            CustSeq = Request.QueryString("CUSTSEQ")

            Bind_Data_togrid_Reason()
        End If
    End Sub

    Sub Bind_Data_togrid_Reason()

        '''' Connect Data to Grid '''''''''''
        Me.dtReason.Columns.Add("USER_CREATED_DATE")
        Me.dtReason.Columns.Add("USER_CREATED_BY")
        Me.dtReason.Columns.Add("REASONS")
        Me.dtReason.Columns.Add("ACTIVE")

        Dim lvsCustType As Integer = Session("KYC")

        Dim qryReason = (From tbName In KYCEntity.MCUST_REASON
                        Where tbName.CUSTSEQ = CustSeq
                       Select tbName.SEQ,
                              tbName.USER_CREATED_DATE,
                              tbName.USER_CREATED_BY,
                              tbName.REASONS,
                              ACTIVE = If(tbName.ACTIVE = "Y", True, False)
                     Order By SEQ)

        For Each mRow In qryReason
            Me.dtReason.Rows.Add(mRow.USER_CREATED_DATE,
                                 mRow.USER_CREATED_BY,
                                 mRow.REASONS,
                                 mRow.ACTIVE)

            Me.dtReason.AcceptChanges()
        Next

        Me.grdReason.DataSource = Me.dtReason
        Me.grdReason.DataBind()
        ''''Number of Rows''''''''''''''''''''''''''''''
        Me.lblCount_Reason.Text = Me.dtReason.Rows.Count
        ''''''''''''''''''''''''''''''''''''''''''''''''
        Session("PopReason") = Me.dtReason

    End Sub

    Protected Sub chk_ACT_REASON_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        ''''check Session ''''''''''''''''''''''
        If Not (Session("PopReason") Is Nothing) Then
            Me.dtReason = Session("PopReason")
        End If

        '''''' assign checkbox value to data table 
        For i = 0 To Me.grdReason.Rows.Count - 1
            Me.dtReason.Rows(i).Item("ACTIVE") = CType(Me.grdReason.Rows(i).Cells(4).FindControl("chk_ACT_REASON"), CheckBox).Checked
        Next i

        Me.dtReason.AcceptChanges()
        Me.grdReason.DataSource = Me.dtReason
        Me.grdReason.DataBind()

        Session("PopReason") = Me.dtReason


    End Sub

    Protected Sub btAddReason_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btAddReason.Click
        Add_To_grid_Reason()
        Save_to_Cust_Reason(Convert.ToInt32(Request.QueryString("CUSTSEQ")))
    End Sub

    Function Validate_Reason() As Boolean
        Validate_Reason = False

        If Me.txtReasonApp.Text = "" Then
            ScriptManager.RegisterStartupScript(txtReasonApp, txtReasonApp.GetType(), "alertMessage", "window.alert('ต้องทำการกรอกข้อมูลให้ครบถ้วนก่อน');", True)
            Exit Function
        End If

        'If clsValid.CheckNull(Me.txtReasonApp) = False Then Exit Function
        Validate_Reason = True
    End Function

    Sub Add_To_grid_Reason()

        If Validate_Reason() = False Then Exit Sub

        'check session of data table
        If Not (Session("PopReason") Is Nothing) Then
            Me.dtReason = Session("PopReason")
        End If
        clsSessionMenuRight = Session("clsMenuRight")
        Dim strUser As String = clsSessionMenuRight.getUserName

        '''''''' insert data to grid row '''''''''''''''''''''''''''''''''''''''''''''''''
        dtReason.Rows.Add(Now,
                          strUser,
                          Me.txtReasonApp.Text,
                          True)

        dtReason.AcceptChanges()
        Me.grdReason.DataSource = Me.dtReason
        Me.grdReason.DataBind()

        ''''Number of Rows''''''''''''''''''''''''''''''
        Me.lblCount_Reason.Text = Me.dtReason.Rows.Count
        ''''''''''''''''''''''''''''''''''''''''''''''''

        ''''deselect 
        Me.grdReason.SelectedIndex = -1

        'now bind datatable to gridview... 
        Session("PopReason") = Me.dtReason

        ''''Clear Screen ''''''''''''
        Me.txtReasonApp.Text = ""
        Me.txtReasonApp.Focus()
    End Sub

    Protected Sub btDelReason_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btDelReason.Click
        Delete_gridRow_grdReason()
        Save_to_Cust_Reason(Convert.ToInt32(Request.QueryString("CUSTSEQ")))
    End Sub

    Sub Delete_gridRow_grdReason()
        Try

            Dim index As Integer = grdReason.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "alert('ต้องเลือกข้อมูลเหตุผลที่ต้องการลบก่อน')", True)
            Else
                'check session of data table
                If Not (Session("PopReason") Is Nothing) Then
                    Me.dtReason = Session("PopReason")
                End If
                ''''remove row at...
                dtReason.Rows.RemoveAt(index)
                dtReason.AcceptChanges()

                ''''bind data again
                grdReason.DataSource = dtReason
                grdReason.DataBind()
                ''''Number of Rows''''''''''''''''''''''''''''''
                Me.lblCount_Reason.Text = Me.dtReason.Rows.Count
                ''''''''''''''''''''''''''''''''''''''''''''''''
                ''''deselect '''''''''''''''
                grdReason.SelectedIndex = -1
                '''''''''''''''''''''''''''''
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Page, _
                                                            Page.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub Save_to_Cust_Reason(ByVal key As Integer)

        Dim mUser_Created_Date As String = ""
        Dim mUser_Created_By As String = ""
        Dim mUser_Reason As String = ""
        Dim chActive As String = ""
        Dim lvsCustType As Integer = Session("KYC")
        '''''''''''''''''''''''

        Try

            Dim DeletedQuery = (From m In KYCEntity.MCUST_REASON
                               Where m.CUSTSEQ = key _
                              Select m).ToList

            If DeletedQuery.Count > 0 Then
                For i As Integer = 0 To DeletedQuery.Count - 1
                    KYCEntity.MCUST_REASON.DeleteObject(DeletedQuery(i))
                Next i
            End If
            ''''''''''''''''''''''
            For Each rowID As GridViewRow In grdReason.Rows
                Dim newCust_Reason As New MCUST_REASON

                ''''get values from Gridview '''''''
                mUser_Reason = rowID.Cells(1).Text : If mUser_Reason = "&nbsp;" Then mUser_Reason = ""
                mUser_Created_Date = rowID.Cells(2).Text : If mUser_Created_Date = "&nbsp;" Then mUser_Created_Date = ""
                mUser_Created_By = rowID.Cells(3).Text : If mUser_Created_By = "&nbsp;" Then mUser_Created_By = ""

                If (CType(rowID.Cells(4).FindControl("chk_ACT_REASON"), CheckBox).Checked = True) Then
                    chActive = "Y"
                Else
                    chActive = "N"
                End If


                ''''''''''''''''''''''''''''''''''''

                '''''''LINQ Start here ''''''''''''''
                newCust_Reason.CUSTSEQ = key
                newCust_Reason.CUSTTYPE = Session("KYC")
                newCust_Reason.SEQ = rowID.RowIndex + 1
                newCust_Reason.REASONS = Mid(mUser_Reason, 1, 150)
                newCust_Reason.USER_CREATED_DATE = mUser_Created_Date
                newCust_Reason.USER_CREATED_BY = mUser_Created_By
                newCust_Reason.ACTIVE = chActive
                newCust_Reason.CREATED_DATE = Now
                newCust_Reason.CREATED_BY = clsSessionMenuRight.getUserName
                KYCEntity.AddToMCUST_REASON(newCust_Reason)
            Next
            KYCEntity.SaveChanges()
        Catch ex As Exception
            Throw ex
            'Catch ex As System.Data.SqlClient.SqlException
            '    Dim lvsERROR As String = ex.Message.ToString
            '    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "clientScript", "alert('" & lvsERROR & "')", True)
        End Try




    End Sub
    
End Class
